package pers.qianyu.module.image.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import pers.qianyu.module.core.domain.image.dto.NoticeDTO;
import pers.qianyu.module.core.domain.image.entity.ImagePO;
import pers.qianyu.module.core.domain.image.enums.NoticeTypeEnum;
import pers.qianyu.module.core.domain.system.vo.SysUserVO;
import pers.qianyu.module.image.listener.events.LikeImageEvent;
import pers.qianyu.module.image.service.NoticeService;
import pers.qianyu.module.system.service.SysUserService;

/**
 * @author mizzle rain
 * @date 2021-02-11 15:06
 */
@Component
public class LikeImageEventListener implements ApplicationListener<LikeImageEvent> {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private NoticeService noticeService;

    @Override
    public void onApplicationEvent(LikeImageEvent likeImageEvent) {
        ImagePO imagePO = likeImageEvent.getImagePO();
        Long operatorId = likeImageEvent.getOperatorId();
        NoticeTypeEnum type = likeImageEvent.getType();

        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setCreatedBy(operatorId);
        noticeDTO.setType(type.getCode());

        SysUserVO sysUserVO = sysUserService.queryById(operatorId);
        noticeDTO.setCreatorName(sysUserVO.getNickname());
        noticeDTO.setCreatorAvatar(sysUserVO.getAvatar());

        noticeDTO.setTargetId(imagePO.getId());
        noticeDTO.setTargetName(imagePO.getTitle());
        noticeDTO.setOwnerId(imagePO.getCreatedBy());

        sysUserVO = sysUserService.queryById(imagePO.getCreatedBy());
        noticeDTO.setOwnerName(sysUserVO.getNickname());

        noticeService.insert(noticeDTO);
    }
}
